<?php

   // Create new service for PHP Remoting as Class
   class GoDBHandler {

      function GoDBHandler () {

         // Define the methodTable for this class in the constructor
         $this->methodTable = array(
                "getUsers" => array(
                   "description" => "Return a list of users",
                   "access" => "remote"
                   )
                );
         }


      function getUsers () {

       	// $mysql = mysql_connect(localhost, "root", "null8fuffzehn");
       	$mysql = mysql_connect(localhost, "GoRSS_DBUser", "Jolc)lp}#u.;o0");
         mysql_select_db( "GoRSS_DB" );

         //return a list of all the users
         $Query = "SELECT * from gorss_users";
         $Result = mysql_query( $Query );

         while ($row = mysql_fetch_object($Result)) {
            $ArrayOfUsers[] = $row;
         }

         return( $ArrayOfUsers );
      }
      
      // category count, later for specific user: SELECT * FROM gorss_cats WHERE uid = $uid, should work this way.
      function getCatCount($cuserid) {
       	// $mysql = mysql_connect(localhost, "root", "null8fuffzehn");
       	$mysql = mysql_connect(localhost, "GoRSS_DBUser", "Jolc)lp}#u.;o0");
      	mysql_select_db("GoRSS_DB");
      	// query only entries where delete-flag is 0 (meaning not deleted):
      	$query = "SELECT * FROM gorss_cats WHERE cdeleted = 0 AND cuserid = '".$cuserid."'";
      	$result = mysql_query($query);
      	while($row = mysql_fetch_object($result)) {
      		$catcount[] = $row;
      	}
      	return($catcount);   	
      }
     
      // reading feeds from a specific category:
      function getFeedsFromCategory($cat, $fuserid) {
       	// $mysql = mysql_connect(localhost, "root", "null8fuffzehn");
       	$mysql = mysql_connect(localhost, "GoRSS_DBUser", "Jolc)lp}#u.;o0");
      	mysql_select_db("GoRSS_DB");
      	$query = "SELECT * FROM gorss_feeds WHERE fcat = $cat AND fuserid = $fuserid AND fdeleted = 0";
      	$result = mysql_query($query);
      	while($row = mysql_fetch_object($result)) {
      		$ArrayOfFeeds[] = $row;
      	}
      	return($ArrayOfFeeds);
      }     
      
      // reading feeds from a specific category:
      function getRecommendedFeedList() {
       	// $mysql = mysql_connect(localhost, "root", "null8fuffzehn");
       	$mysql = mysql_connect(localhost, "GoRSS_DBUser", "Jolc)lp}#u.;o0");
      	mysql_select_db("GoRSS_DB");
      	$query = "SELECT * FROM gorss_rec_feeds WHERE rdeleted = 0";
      	$result = mysql_query($query);
      	while($row = mysql_fetch_object($result)) {
      		$reclist[] = $row;
      	}
      	return($reclist);
      }
	
      // if new category specified, first make cat-entry, then feed entry (following function):
      function setNewCategoryEntry($cname, $cdeleted, $cuserid) {
            $mysql = mysql_connect(localhost, "GoRSS_DBUser", "Jolc)lp}#u.;o0");
            mysql_select_db("GoRSS_DB");
            return mysql_query("INSERT INTO gorss_cats (cname, cdeleted, cuserid) VALUES ('".$cname."', '".$cdeleted."', '".$cuserid."');");
      }
      
      // called when user edits/deletes feed categories (sets only a pseudo-flag, so categories will not really be lost).
      function deleteCategory($cname, $cdeleted, $cuserid) {
      	$mysql = mysql_connect(localhost, "GoRSS_DBUser", "Jolc)lp}#u.;o0");
      	mysql_select_db("GoRSS_DB");
//      	return mysql_query("DELETE FROM gorss_cats WHERE cname = '".$cname."'");
      	return mysql_query("UPDATE gorss_cats SET cdeleted = '".$cdeleted."' WHERE cname = '".$cname."' AND  cuserid = '".$cuserid."'");
      }
      
      // called when user wants to delete feeds which are selected by the checkboxes in the datagrid:
      function deleteFeed($fid, $fdeleted) {
      	$mysql = mysql_connect(localhost, "GoRSS_DBUser", "Jolc)lp}#u.;o0");
      	mysql_select_db("GoRSS_DB");
//      	return mysql_query("DELETE FROM gorss_cats WHERE cname = '".$cname."'");
      	return mysql_query("UPDATE gorss_feeds SET fdeleted = '".$fdeleted."' WHERE fid = '".$fid."'");
//      	return mysql_query("UPDATE gorss_feeds SET fdeleted = '".$fdeleted."' WHERE fid = '".$fid."' AND  fuserid = '".$fuserid."'");
      }
      
	  // go make new feed entry:
	  function setNewFeedEntry($furl, $fname, $fcat, $fuserid) {
	  	// handling new feed entries.
	  	$mysql = mysql_connect(localhost, "GoRSS_DBUser", "Jolc)lp}#u.;o0");
      	  	mysql_select_db("GoRSS_DB");
      	  	return mysql_query("INSERT INTO gorss_feeds (furl, fname, fcat, fuserid) VALUES ('".$furl."','".$fname."','".$fcat."','".$fuserid."');");
	  }

      // go make new user entry:
      function setNewUserEntry($uname, $umail, $upass) {
            // handling new user entries.
            $mysql = mysql_connect(localhost, "GoRSS_DBUser", "Jolc)lp}#u.;o0");
            mysql_select_db("GoRSS_DB");
            return mysql_query("INSERT INTO gorss_users (uname, umail, upass) VALUES ('".$uname."','".$umail."','".md5($upass)."');");
      }

	  
	  // go mark article as read:
	  function setArticleVisited($auserid, $alink) {
	  	$mysql = mysql_connect(localhost, "GoRSS_DBUser", "Jolc)lp}#u.;o0");
	  	mysql_select_db("GoRSS_DB");
	  	// insert some comparing functionality which checks if $alink already exists in combination 
	  	// with the &auserid; if yes: don't make no entry as it would be redundant.
	  	$tempresult = mysql_query("SELECT * FROM gorss_articles WHERE auserid = $auserid AND alink = $alink");
	  	$numrows = mysql_num_rows($tempresult);
	  	if($numrows == 0) {
	  		return mysql_query("INSERT INTO gorss_articles (auserid, alink) VALUES ($auserid, $alink);");
	  	}
	  }
	  
	  // go check which articles are read, returns read objects.
	  function getArticlesVisited($feedLinks) {
       	$mysql = mysql_connect(localhost, "GoRSS_DBUser", "Jolc)lp}#u.;o0");
      	mysql_select_db("GoRSS_DB");
      	
      	$arrLength = count($feedLinks);
      	for($ii = 0; $ii < $arrLength; $ii++) {
       	 $result = mysql_query("SELECT * FROM gorss_articles WHERE alink = '".$feedLinks[$ii]."'");
      		while($row = mysql_fetch_object($result)) {
      			$visited[] = $row;
      		}
      	}
      	return($visited);
      	}
      	
      	  
	  // called when a user logs in.
	  function userLogin($uname, $upass) {
	  	$true = false;
	  	$mysql = mysql_connect(localhost, "GoRSS_DBUser", "Jolc)lp}#u.;o0");
	  	mysql_select_db("GoRSS_DB");
	  	$query = "SELECT * FROM gorss_users WHERE uname = $uname";
	  	// mysql_query($check);
	  	$result = mysql_query($query);
	  	$row = mysql_fetch_object($result);
	  	$pass = $row["upass"];
	  	// return $pass;
	  	if($upass == $pass) {
	  		$cont = true;
	  		// return true;
	  	} else {
	  		$cont = false;
	  		// return false;
	  	}
	  	return $cont;
      	/*while($row = mysql_fetch_object($result)) {
      		$arr[] = $row;
      	}
      	if($arr[2] == $pass) {
      		$true = true;
      		return $true;
      	}*/
	  	// $pass == $upass ? return true : return false;
	  }

	function validate($nfo){
		$dbc = mysql_connect("localhost", "GoRSS_DBUser", "Jolc)lp}#u.;o0");
		mysql_select_db("GoRSS_DB");
 
		$user = $nfo[0];
		$pass = $nfo[1];
 
		$query = "SELECT * FROM gorss_users WHERE uname = $user AND upass = $pass";
		$rs = mysql_query($query);
 
		$var = mysql_fetch_object($rs);
		$u = $var["uname"];
		$p = $var["upass"];
 
		if ($pass != $p){
			$response[0] = "INVALID";
			$response[1] = "Invalid username and/or password";
		} else {
			$response[0] = "VALID";
			$response[1] = "Welcome $u";
		}
		return $response;
	}
	

 
  
}
?>